package com.yuandian.management.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.management.entity.ConfAlarm;
import com.yuandian.api.management.validate.InsertFlag;
import com.yuandian.api.management.validate.UpdateFlag;
import com.yuandian.api.management.vo.AlarmsVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.log.annotation.SysLog;
import com.yuandian.management.service.ConfAlarmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
/**
 * 告警信息-Controller
 * @author zfsn
 * @date 2023-4-4
 */
@Api(tags = "告警配置管理")
@RestController
@RequestMapping("/alarm")
public class ConfAlarmController {

	@Autowired
	private ConfAlarmService confAlarmService;

	/******					自定义告警配置										***/


	/**
	 * 通过主键查询数据
	 * @param idList 主键集合
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键查询数据", notes = "通过主键查询数据，支持批量查询")
	@GetMapping("/{ids}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ids", value = "id(支持多选)", paramType = "query", required = true, dataType = "Integer")
	})
	public R<List<ConfAlarm>> queryByIds(@PathVariable("ids") List<Integer> idList) {
		return R.ok(confAlarmService.listByIds(idList));
	}




	/**
	 * 分页查询
	 * @param confAlarm 筛选条件
	 * @return 通用返回对象
	 * @Description type = 2, 告警类型为自定义告警， isEnable = 0表示启动该告警监控
	 */
	@ApiOperation(value = "自定义告警分页查询", notes = "自定义告警分页查询")
	@PostMapping("/customAlarm")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "isEnable", value = "启用状态: 0-启用 1-禁用", paramType = "body", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "alarmIndicatorIds", value = "告警指标(多选)", paramType = "body", required = false, dataType = "Integer"),
			@ApiImplicitParam(name = "alarmTemplateIds", value = "告警信息模板(多选)", paramType = "body", required = false, dataType = "Integer"),
	})
	public R<Page<ConfAlarm>> listPageByCustomAlarm(@RequestBody @ApiIgnore ConfAlarm confAlarm) {
		confAlarm.setType(2);
		return R.ok(confAlarmService.listPage(confAlarm));
	}

	/**
	 * 新增自定义告警数据
	 * @param confAlarmVo 实例对象
	 * @return 通用返回对象
	 * @Description type = 2, 告警类型为自定义告警
	 */
	@ApiOperation(value = "新增自定义告警数据", notes = "新增自定义告警数据")
	@PostMapping("/customAlarm/save")
	public R<String> saveCustomAlarm(@RequestBody AlarmsVo confAlarmVo) {
		confAlarmVo.setType(2);
		return confAlarmService.saveConfig(confAlarmVo);
	}

	/**
	 * 通过主键更新自定义告警数据
	 * @param confAlarmVo 实例对象
	 * @return 通用返回对象
	 * @Description type = 2, 告警类型为自定义告警
	 */
	@ApiOperation(value = "通过主键更新自定义告警数据", notes = "通过主键更新自定义告警数据")
	@PutMapping("/customAlarm")
	public R<String> updateCustomAlarmById(@RequestBody AlarmsVo confAlarmVo) {
		confAlarmVo.setType(2);
		return confAlarmService.updateConfigById(confAlarmVo);
	}

	/**
	 * 通过主键删除数据
	 * @param ids 主键
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "通过主键删除数据", notes = "通过主键删除数据")
	@SysLog("删除告警信息数据")
	@DeleteMapping("/delete/{ids}")
	public R<Boolean> removeByIds(@PathVariable("ids") List<Integer> ids){
		return R.ok(confAlarmService.removeByIds(ids));
	}


	/**
	 * 修改是否启用状态
	 *
	 * @param confAlarm
	 * @return 结果
	 * @Description type = 2, 告警类型为自定义告警
	 */
	@PostMapping("/customAlarm/editAlarmEnable")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "isEnable", value = "启用状态: 0-启用 1-禁用", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "alarmIndicatorId", value = "告警指标", paramType = "body", required = true, dataType = "Integer")
	})
	public R<String> editAlarmEnable(@ApiIgnore @RequestBody ConfAlarm confAlarm) {
		confAlarm.setType(2);
		return confAlarmService.updateAlarmEnable(confAlarm);
	}



	/******					基础告警配置										***/


	/**
	 * 基础告警分页查询
	 * @param confAlarm 筛选条件
	 * @return 通用返回对象
	 * @Description type = 1, 告警类型为基础告警， isEnable = 0表示启动该告警监控
	 */
	@ApiOperation(value = "基础告警分页查询", notes = "基础告警分页查询")
	@PostMapping("/baseAlarm")
	public R<Page<ConfAlarm>> listPageByBaseAlarm(@RequestBody ConfAlarm confAlarm) {
		confAlarm.setType(1);
		return R.ok(confAlarmService.listPage(confAlarm));
	}

	/**
	 * 查询当前模板是否被告警引用到
	 *
	 * @param id
	 *
	 * @return 通用返回对象
	 */
	@GetMapping("/queryAlarmByTemplateId/{id}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", paramType = "query", required = true, dataType = "Integer"),
	})
	public R queryAlarmByTemplateId(@PathVariable("id") Integer id) {
		return confAlarmService.queryAlarmByTemplateId(id);
	}

	/**
	 * 新增基础告警数据
	 * @param confAlarmVo 实例对象
	 * @return 通用返回对象
	 * @Description type = 1, 告警类型为基础告警
	 */
	@ApiOperation(value = "新增基础告警数据", notes = "新增基础告警数据")
	@PostMapping("/baseAlarm/save")
	public R<String> saveBaseAlarm(@Validated({InsertFlag.class}) @RequestBody AlarmsVo confAlarmVo) {
		confAlarmVo.setType(1);
		return confAlarmService.saveConfig(confAlarmVo);
	}


	/**
	 * 通过主键更新基础告警数据
	 * @param confAlarmVo 实例对象
	 * @return 通用返回对象
	 * @Description type = 1, 告警类型为基础告警
	 */
	@ApiOperation(value = "通过主键更新基础告警数据", notes = "通过主键更新基础告警数据")
	@PutMapping("/baseAlarm")
	public R<String> updateBaseAlarmById(@Validated({UpdateFlag.class}) @RequestBody AlarmsVo confAlarmVo) {
		confAlarmVo.setType(1);
		return confAlarmService.updateConfigById(confAlarmVo);
	}

	/**
	 * 查询指标类型(远程调用接口)
	 * @param category 实例对象
	 * @return 通用返回对象
	 */
	@ApiOperation(value = "查询指标类型(远程调用接口)", notes = "查询指标类型(远程调用接口)")
	@GetMapping("/queryIndicator/{category}")
	public R<List<Integer>> queryIndicator(@PathVariable("category") String category) {
		return R.ok(confAlarmService.queryIndicator(category));
	}


	/**
	 * 查询当前指标是否被告警引用到
	 *
	 * @param id
	 *
	 * @return 通用返回对象
	 */
	@GetMapping("/queryAlarmByIndicatorId/{id}")
	@ApiOperation(value = "查询当前指标是否被告警引用到", notes = "查询当前指标是否被告警引用到")
	public R queryAlarmByIndicatorId(@PathVariable("id") Integer id) {
		return confAlarmService.queryAlarmByIndicatorId(id);
	}
}